﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;

namespace SansaImageConverter
{
    /// <summary>
    /// Tools
    /// </summary>
    public static class Tools
    {
		#region P/Invoke Methods (1) 

        /// <summary>
        /// Beeps the specified frequency.
        /// </summary>
        /// <param name="dwFreq">Frequency of the beep.</param>
        /// <param name="dwDuration">Duration of the beep.</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int Beep(int dwFreq, int dwDuration);

		#endregion P/Invoke Methods 

		#region Methods (4) 

        /// <summary>
        /// Beeps this instance.
        /// </summary>
        public static void Beep()
        {
            int freqency = 440; // hertz
            int duration = 500; // milliseconds
            Beep(freqency, duration);
        }

        /// <summary>
        /// Converts the image.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="rotate">If set to <c>true</c> rotate image.</param>
        /// <param name="sizing">Sizing.</param>
        /// <param name="quality">Quality.</param>
        /// <returns></returns>
        public static Bitmap ConvertImage(string fileName, bool rotate, int sizing, InterpolationMode quality)
        {
            const int WIDTH = 176;
            const int HEIGHT = 220;

            Bitmap source = (Bitmap)Bitmap.FromFile(fileName);
            Bitmap output = new Bitmap(WIDTH, HEIGHT, PixelFormat.Format16bppRgb565);

            if (rotate & (source.Width > source.Height))
                source.RotateFlip(RotateFlipType.Rotate90FlipNone);
            source.RotateFlip(RotateFlipType.Rotate180FlipX);

            Point pos;
            Size size;

            if (sizing == 1)
            {
                pos = new Point(0, 0);
                size = new Size(WIDTH, HEIGHT);
            }
            else
            {
                double ratio = (double)source.Width / (double)source.Height;
                if (ratio >= ((double)WIDTH / (double)HEIGHT))
                    size = new Size(WIDTH, (int)((double)WIDTH / source.Width * source.Height));
                else
                    size = new Size((int)((double)HEIGHT / source.Height * source.Width), HEIGHT);
                pos = new Point((int)(WIDTH - size.Width) / 2, (int)(HEIGHT - size.Height) / 2);
            }

            Graphics g = Graphics.FromImage(output);
            g.InterpolationMode = quality;
            g.FillRectangle(Brushes.Black, new Rectangle(0, 0, WIDTH, HEIGHT));
            g.DrawImage(source, new Rectangle(pos, size));

            return output;
        }

        /// <summary>
        /// Gets the image list.
        /// </summary>
        /// <param name="path">Path.</param>
        /// <returns></returns>
        public static string[] GetImageList(string path)
        {
            DirectoryInfo info = new DirectoryInfo(path);
            List<string> list = new List<string>();
            list.AddRange(Directory.GetFiles(path, "*.jpg"));
            list.AddRange(Directory.GetFiles(path, "*.jpeg"));
            list.AddRange(Directory.GetFiles(path, "*.bmp"));
            list.AddRange(Directory.GetFiles(path, "*.gif"));
            list.AddRange(Directory.GetFiles(path, "*.png"));
            list.AddRange(Directory.GetFiles(path, "*.tif"));
            list.AddRange(Directory.GetFiles(path, "*.tiff"));
            list.AddRange(Directory.GetFiles(path, "*.emf"));
            list.AddRange(Directory.GetFiles(path, "*.wmf"));
            list.AddRange(Directory.GetFiles(path, "*.ico"));
            return list.ToArray();
        }

		#endregion Methods 
    }
}
